<template>
  <div class="role-list">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-form ref="form" :model="form">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primafy" :disabled="loading" @click="onSubmit"
              >查询</el-button
            >
            <el-button :disabled="loading" @click="onReset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-button @click="add">添加角色</el-button>

      <el-table :data="roles" style="width: 100%" v-loading="loading">
        <el-table-column prop="id" label=""></el-table-column>
        <el-table-column prop="name" label=""></el-table-column>
        <el-table-column prop="desciption" label=""></el-table-column>
        <el-table-column prop="createdTime" label=""></el-table-column>
        <el-table-column label="操作" align="center" width="150px">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="$router.push({
                name: 'alloc-menu',
                params: {
                  roleId: scope.row.id
                }
              })"
              >分配菜单</el-button
            >
            <el-button
              type="text"
              @click="delete (scope.$index, scope.row)"
              >分配资源</el-button
            >
            <el-button
              @click="edit(scope.row)"
              type="text"
              >编辑</el-button
            >
            <el-button
              @click="delete (scope.$index, scope.row)"
              type="text"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog title="添加角色" :visible.sync="dialogFormVisible" width="80%">
      <create-or-edit
      v-if="dialogFormVisible"
      @success="onSuccess"
      @cancel="diagFormVisible=false"
      :role-id="roleId"
      :is-edit="isEdit"
      />
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import CreateOrEdit from './CreateOrEdit.vue'
import { getRoles } from '@/services/role'
export default Vue.extend({
  name: 'RoleList',
  data () {
    return {
      roles: [],
      form: {
        current: 1,
        size: 10,
        name: ''
      },
      loading: false,
      dialogFormVisible: false,
      roleId: null,
      isEdit: false
    }
  },
  components: {
    CreateOrEdit
  },
  created () {
    this.loadRoles()
  },
  methods: {
    async loadRoles () {
      this.loading = true
      const { data } = await getRoles(this.form)
      this.roles = data.data.records
      this.loading = false
    },
    onSubmit () {
      //
    },
    onReset () {
      //
    },
    onSuccess () {
      this.dialogFormVisible = false
      this.loadRoles()
    },
    edit (data: any) {
      this.dialogFormVisible = true
      this.roleId = data.id
      this.isEdit = true
    },
    add () {
      this.dialogFormVisible = true
      this.isEdit = false
    }
  }
})
</script>

<style lang="scss" scoped>
</style>
